package com.lyw.login.vm

import androidx.lifecycle.viewModelScope
import com.lu.library.base.BaseViewModel
import com.lu.library.base.IUiIntent
import com.lu.library.base.IUiState
import com.lu.library.ext.launchSafe
import com.lu.library.http.ApiCoroutineExceptionHandler
import com.lu.library.kv.KV
import com.lu.library.kv.KVDelegate
import com.lu.library.utils.CountDown
import com.lyw.login.Constants
import com.lyw.login.api.LoginApi
import com.lyw.login_api.LoginConstants
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

sealed class ForgetPwdUiState:IUiState{
    data class INIT(val phone: String,var enableCode: Boolean,var enableLogin:Boolean) : ForgetPwdUiState()
    data class LoginSuccess(var i:Int) : ForgetPwdUiState()
    data class CountDown(var str:String,var enableCode:Boolean):ForgetPwdUiState()
    data class LoginEnable(var loginEnable: Boolean):ForgetPwdUiState();
    data class SendCodeEnable(var codeEnable: Boolean):ForgetPwdUiState();
    data class LoginFail(var msg:String):ForgetPwdUiState()
}

sealed class ForgetPwdIntent:IUiIntent{
    data class ChangePwdAndLoginIn(val phone: String, val pwd: String,val code:String) : ForgetPwdIntent()
    data class SendCode(val phone:String): ForgetPwdIntent()
    data class PhoneChange(var phone:String):ForgetPwdIntent()
    data class CodeChange(var code:String):ForgetPwdIntent()
    data class PwdChange(var pwd:String):ForgetPwdIntent()
    data class Pwd2Change(var pwd2:String):ForgetPwdIntent()

}
@HiltViewModel
class ForgetPwdVm  @Inject constructor(
    val kv:KV,
    val api:LoginApi
):BaseViewModel<ForgetPwdUiState,ForgetPwdIntent>() {

    private var phone:String = ""
    private var pwd:String = ""
    private var pwd2:String =""
    private var code:String = ""
    //是否在倒计时中
    private var isCoding =false


    init {
        updateUiState {
            ForgetPwdUiState.INIT(phone,phone.isNotEmpty(),phone.isNotEmpty()&&code.isNotEmpty());
        }
    }

    override fun initUiState(): ForgetPwdUiState {
        return ForgetPwdUiState.INIT("",false,false);
    }

    override fun handleIntent(intent: ForgetPwdIntent) {
        when(intent){
            is ForgetPwdIntent.PwdChange->{
                pwd = intent.pwd
                updateUiState {
                    ForgetPwdUiState.LoginEnable(phone.isNotEmpty()&&code.isNotEmpty()
                            && pwd.isNotEmpty()&&pwd2.isNotEmpty())
                }
            }
            is ForgetPwdIntent.Pwd2Change->{
                pwd2 = intent.pwd2
                updateUiState {
                    ForgetPwdUiState.LoginEnable(phone.isNotEmpty()&&code.isNotEmpty()
                            && pwd.isNotEmpty()&&pwd2.isNotEmpty())
                }
            }
            is ForgetPwdIntent.ChangePwdAndLoginIn->{
                login(intent)
            }
            is ForgetPwdIntent.SendCode->{
                viewModelScope.launch {
                    isCoding = true
                    CountDown.start(6,{t->
                        updateUiState {
                            ForgetPwdUiState.CountDown(t.toString()+"s",false)
                        }
                    }){
                        isCoding = false
                        updateUiState {
                            ForgetPwdUiState.CountDown("发送验证码",true)
                        }
                    }
                }

            }
            is ForgetPwdIntent.CodeChange->{
                code = intent.code
                updateUiState {
                    ForgetPwdUiState.LoginEnable(phone.isNotEmpty()&&code.isNotEmpty()
                            && pwd.isNotEmpty()&&pwd2.isNotEmpty())
                }
            }
            is ForgetPwdIntent.PhoneChange->{
                phone = intent.phone
                updateUiState {
                    ForgetPwdUiState.LoginEnable(phone.isNotEmpty()&&code.isNotEmpty()
                            && pwd.isNotEmpty()&&pwd2.isNotEmpty())
                }
                if (!isCoding) {
                    updateUiState {
                        ForgetPwdUiState.SendCodeEnable(phone.isNotEmpty())
                    }
                }
            }
            else ->{

            }
        }
    }
    var index=0
    private fun login(intent:ForgetPwdIntent.ChangePwdAndLoginIn){
        if (pwd!=pwd2){
            updateUiState {
                ForgetPwdUiState.LoginFail("两次输入密码不对")
            }
            return
        }
        viewModelScope.launchSafe(Dispatchers.Main) {
//            val userInfo = withContext(Dispatchers.IO) {
//                api.login(LoginReq(intent.name, intent.pwd)).data
//            }
//            userInfo?.let {
//                isLogin = true
//                kv.put(LoginConstants.KEY_USER_INFO,GsonUtils.toJson(it))
//            }
//            LogUtils.d(userInfo)
            index++
            updateUiState {
                ForgetPwdUiState.LoginSuccess(index/2);
            }

        }
    }
}